﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Spi;
using NGuice.Inject.Internal.Util;

namespace NGuice.Inject.Internal
{
    internal class UntargettedBindingImpl<T> : BindingImpl<T>, IUntargettedBinding<T>
    {
        internal UntargettedBindingImpl(InjectorImpl injector, Key<T> key, object source)
            : base(injector, key, source, new UntargettedBindingImplInternalFactory<T>(), Scoping.UNSCOPED)
        {
        }

        public UntargettedBindingImpl(object source, Key<T> key, Scoping scoping)
            : base(source, key, scoping)
        {
        }

        public override V AcceptTargetVisitor<V>(IBindingTargetVisitor<T, V> visitor)
        {
            return visitor.Visit(this);
        }

        protected internal override BindingImpl<T> WithScoping(Scoping scoping)
        {
            return new UntargettedBindingImpl<T>(Source, Key, scoping);
        }

        protected internal override BindingImpl<T> WithKey(Key<T> key)
        {
            return new UntargettedBindingImpl<T>(Source, key, Scoping);
        }

        public override void ApplyTo(IBinder binder)
        {
            Scoping.ApplyTo(binder.WithSource(Source).Bind(Key));
        }

        public override string ToString()
        {
            return new ToStringBuilder(typeof(IUntargettedBinding<T>))
                .Add("key", Key)
                .Add("source", Source)
                .ToString();
        }

        public override bool Equals(object obj)
        {
            if (obj is UntargettedBindingImpl<T>)
            {
                UntargettedBindingImpl<T> o = (UntargettedBindingImpl<T>)obj;
                return Key.Equals(o.Key)
                  && Scoping.Equals(o.Scoping);
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return Objects.GetHashCode(Key, Scoping);
        }
    }

    internal class UntargettedBindingImplInternalFactory<T> : InternalFactory<T>
    {
        public T Get(Errors errors, InternalContext context, Dependency dependency, bool linked)
        {
            throw new AssertionError();
        }

        object InternalFactory.Get(Errors errors, InternalContext context, Dependency dependency, bool linked)
        {
            return Get(errors, context, dependency, linked);
        }
    }
}
